package restful

import (
	"context"
	"net/http"

	v1 "gitee.com/lcc_dr/restful-api-demo/api/book/v1"
	"github.com/emicklei/go-restful/v3"
	"github.com/go-playground/validator/v10"
)

// POST http://localhost:8080/book
//
func (u *HandleRestService) CreateBook(request *restful.Request, response *restful.Response) {

	var ctx context.Context
	createBookReq := v1.CreateBookRequest{}
	err := request.ReadEntity(&createBookReq)

	if err == nil {
		validate = validator.New()

		err := validate.Struct(createBookReq)
		if err != nil {
			response.WriteError(http.StatusInternalServerError, err)
			return

		}
		replay, err := u.svc.CreateBook(ctx, &createBookReq)
		if err != nil {
			response.WriteError(http.StatusInternalServerError, err)
		}

		response.WriteEntity(replay)
	} else {
		response.WriteError(http.StatusInternalServerError, err)
	}

}

// GET http://localhost:8080/book?page={}&keywords={}
//
func (u *HandleRestService) QueryBook(request *restful.Request, response *restful.Response) {
	var ctx context.Context
	u.l.Info("get book list request")
	queryBookReq := v1.QueryBookRequest{}
	err := request.ReadEntity(&queryBookReq)
	if err == nil {
		replay, err := u.svc.QueryBook(ctx, &queryBookReq)
		if err != nil {
			response.WriteError(http.StatusInternalServerError, err)
		}

		response.WriteEntity(replay)
	} else {
		response.WriteError(http.StatusInternalServerError, err)
	}
}

// GET http://localhost:8080/book/1
//
func (u *HandleRestService) DescribeBook(request *restful.Request, response *restful.Response) {
	// var ctx context.Context
	u.l.Info("get book describe request")
	// id := request.PathParameter("user-id")

	// describeBookReq := v1.DescribeBookRequest{Id: id}

	replay := v1.Book{
		Id:       "11",
		CreateAt: 2022,
		UpdateAt: 2022,
		UpdateBy: "lcc",
		Data: &v1.CreateBookRequest{
			Name:     "lcc",
			Author:   "lcc",
			CreateBy: "lcc",
		},
	}
	// err := request.ReadEntity(&describeBookReq)
	// if err == nil {
	// 	replay, err := u.svc.DescribeBook(ctx, &describeBookReq)
	// 	if err != nil {
	// 		response.WriteError(http.StatusInternalServerError, err)
	// 	}

	response.WriteAsJson(replay)
	// response.WriteAsXml(replay)
	// } else {
	// 	response.WriteError(http.StatusInternalServerError, err)
	// }
}

// PUT http://localhost:8080/book/1
// <User><Id>1</Id><Name>Melissa</Name></User>
//
func (u *HandleRestService) UpdateBook(request *restful.Request, response *restful.Response) {
	var ctx context.Context
	updateBookReq := v1.UpdateBookRequest{}
	err := request.ReadEntity(&updateBookReq)
	if err == nil {
		replay, err := u.svc.UpdateBook(ctx, &updateBookReq)
		if err != nil {
			response.WriteError(http.StatusInternalServerError, err)
		}

		response.WriteEntity(replay)
	} else {
		response.WriteError(http.StatusInternalServerError, err)
	}
}

// DELETE http://localhost:8080/users/1
//
func (u *HandleRestService) DeleteBook(request *restful.Request, response *restful.Response) {
	var ctx context.Context
	deleteBookReq := v1.DeleteBookRequest{}
	err := request.ReadEntity(&deleteBookReq)
	if err == nil {
		replay, err := u.svc.DeleteBook(ctx, &deleteBookReq)
		if err != nil {
			response.WriteError(http.StatusInternalServerError, err)
		}

		response.WriteEntity(replay)
	} else {
		response.WriteError(http.StatusInternalServerError, err)
	}
}
